package com.hongxun.modules.api.rest;

import com.alibaba.fastjson.JSON;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;
import com.hongxun.modules.csg.entity.CPBaseEntity;
import com.hongxun.modules.csg.service.CPBaseService;
import com.hongxun.modules.sys.controller.AbstractController;
import com.hongxun.modules.sys.entity.SysDeptEntity;
import com.hongxun.modules.sys.entity.SysRoleEntity;
import com.hongxun.modules.sys.entity.SysUserEntity;
import com.hongxun.modules.sys.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 施工项目表
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-09-11 11:18:23
 */
@RestController
@RequestMapping("/rest/cpbase")
public class CPBaseRestController extends AbstractController {
	@Autowired
	private CPBaseService cPBaseService;
	@Autowired
	private SysRoleDeptService sysRoleDeptService;
	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Autowired
	protected ShiroService shiroService;
	@Autowired
	private SysDeptService sysDeptService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysRoleService sysRoleService;

	
	/**
	 * 获取所有施工项目
	 */
	@RequestMapping("/getAllProjects")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
		Map<String , Object> cPBaseMap = new HashMap<>();

		boolean myprio = false;
		SysUserEntity sysUserEntity = shiroService.queryByToken(params.get("token").toString());
		SysUserEntity sysus =  sysUserService.queryObject(sysUserEntity.getUserId());
		SysDeptEntity sysde = sysDeptService.queryObject(sysus.getDeptId());
		String wheres = " 1=1 and p_b_datastate !='删除' ";
		if (sysde.getParentId() == 17) {
//			myprio = false;

			List<Long> roleIdList = sysUserRoleService.queryRoleIdList(sysUserEntity.getUserId());
			String deptId = "";

			for (Long id : roleIdList) {
				SysRoleEntity sysRoleEntity = sysRoleService.queryObject(id);
				if(sysRoleEntity.getRemark().equals("是"))
				{
					myprio=false;
				}else{
					myprio=true;
				}
				List<Long> deptIdList = sysRoleDeptService.queryDeptIdList(id);
				for (Long sid : deptIdList) {
					deptId += sid + ",";
				}
			}
			if(deptId.length()>0)
			{
				deptId =deptId.substring(0,deptId.length()-1);
				wheres += " and  p_b_orgid in ("+deptId+")";
			}
			else
			{
				wheres += " and  p_b_orgid in ( -1 )";
			}

		}
		if (!myprio) {
			wheres = " and (p_b_state = '10' or p_b_state = '20') and FIND_IN_SET('" + sysUserEntity.getUserId() + "',p_b_collectid)";
		}else{
			wheres = " and (p_b_state = '10' or p_b_state = '20') and FIND_IN_SET('" + sysUserEntity.getUserId() + "',p_b_reckonid)";

		}
		cPBaseMap.put("filterSql",wheres+" ");

		List<CPBaseEntity> cpBaseEntityList = cPBaseService.queryList(cPBaseMap);

		return R.ok().put("cpBaseEntityList", JSON.toJSONString(cpBaseEntityList));
	}

	
}
